#include "GraphicLCD.h"


// Graphic Serial LCD 128x64
// Arduino Pin 16 (TX) to LCD Pin RX
// Arduino Pin Vin to LCD Vin (Assuming you're powering Arduino externally with 9 VDC)
// Arduino Pin Gnd to LCD Pin Gnd

SoftwareSerial lcdSerial2(17, 16);


GraphicLCD::GraphicLCD()
{
	lcdSerial2.begin(19200);
}

void GraphicLCD::print(char *data)
{
	//lcdSerial2.begin(19200);
	lcdSerial2.print(data);
}

void GraphicLCD::printat(byte xpos, byte ypos, String data)
{
	lcdSerial2.write(0x7C);
	lcdSerial2.write(0x18);
	lcdSerial2.write(xpos);

	lcdSerial2.write(0x7C);
	lcdSerial2.write(0x19);
	lcdSerial2.write(ypos);

	lcdSerial2.print(data);
}

void GraphicLCD::clearScreen(){
  lcdSerial2.write(0x7C);
  lcdSerial2.write(byte(0x00));
  //lcdSerial2.print('0x00');
}

void GraphicLCD::demo(){
  lcdSerial2.write(0x7C);
  lcdSerial2.write(0x04);
}

void GraphicLCD::toggleSplashScreen(){
  lcdSerial2.write(0x7C);
  lcdSerial2.write(0x13);
}

void GraphicLCD::setBackgroundBrightness(byte value){
  lcdSerial2.write(0x7C);
  lcdSerial2.write(0x02);
  lcdSerial2.write(value);
}

void GraphicLCD::setLCDBaudRate(long value){
  // Get the internal reference for this baud rate
  char *ref = " ";
  if(value == 4800)
    ref = "1";
  else if(value == 9600)
    ref = "2";
  else if(value == 19200)
    ref = "3";
  else if(value == 38400)
    ref = "4";
  else if(value == 57600)
    ref = "5";
  else if(value == 115200)
    ref = "6";
  else
    return;

  // Since it often rolls back to 115200, try setting it via 115200 first
  lcdSerial2.begin(115200);
  lcdSerial2.write(0x7C);
  lcdSerial2.write(0x07);
  lcdSerial2.print(ref);

  // Now change the serial port to the desired rate, and set it again.
  lcdSerial2.begin(value);
  lcdSerial2.write(0x7C);
  lcdSerial2.write(0x07);
  lcdSerial2.print(ref);
}

void GraphicLCD::setX(byte value){
  lcdSerial2.write(0x7C);
  lcdSerial2.write(0x18);
  lcdSerial2.write(value);
}

void GraphicLCD::setY(byte value){
  lcdSerial2.write(0x7C);
  lcdSerial2.write(0x19);
  lcdSerial2.write(value);
}

void GraphicLCD::setPixel(byte state){
  lcdSerial2.write(0x50);
  lcdSerial2.write(0x40);
  lcdSerial2.write(state);
}

void GraphicLCD::drawLine(byte startX, byte startY, byte endX, byte endY, byte state){
  lcdSerial2.write(0x7C);
  lcdSerial2.write(0x0C);
  lcdSerial2.write(startX);
  lcdSerial2.write(startY);
  lcdSerial2.write(endX);
  lcdSerial2.write(endY);
  lcdSerial2.write(state);
}

void GraphicLCD::drawCircle(byte startX, byte startY, byte radius, byte state){
  lcdSerial2.write(0x7C);
  lcdSerial2.write(0x03);
  lcdSerial2.write(startX);
  lcdSerial2.write(startY);
  lcdSerial2.write(radius);
  lcdSerial2.write(state);
}

void GraphicLCD::drawBox(byte topLeftX, byte topLeftY, byte bottomRightX, byte bottomRightY, byte state){
  lcdSerial2.write(0x7C);
  lcdSerial2.write(0x0F);
  lcdSerial2.write(topLeftX);
  lcdSerial2.write(topLeftY);
  lcdSerial2.write(bottomRightX);
  lcdSerial2.write(bottomRightY);
  lcdSerial2.write(state);
}

void GraphicLCD::eraseBox(byte topLeftX, byte topLeftY, byte bottomRightX, byte bottomRightY, byte state){
  lcdSerial2.write(0x7C);
  lcdSerial2.write(0x05);
  lcdSerial2.write(topLeftX);
  lcdSerial2.write(topLeftY);
  lcdSerial2.write(bottomRightX);
  lcdSerial2.write(bottomRightY);
  lcdSerial2.write(state);
}
